"use strict";

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
};
//字符串格式化
String.prototype.format = function(args) {
  let result = this;
  if (arguments.length < 1) {
    return result.toString();
  }

  let data = arguments;
  if (arguments.length == 1 && typeof args == "object") {
    data = args;
  }
  for (let key in data) {
    let value = data[key];
    if (undefined != value) {
      result = result.replace("{" + key + "}", value);
    }
  }
  return result.toString();
};
// 判断是否在给定时间范围内
Date.prototype.inRange = function(tr) {
  if (tr[0].getTime() < this.getTime() && tr[1].getTime() > this.getTime()) {
    return true;
  }
  return false;
};
// 一天的毫秒数
Date.prototype.oneDay = 1000 * 60 * 60 * 24;
// 日期格式化
Date.prototype.format = function(fmt) {
  return format(this, fmt);
  // let o = {
  //   "M+": this.getMonth() + 1, //月份
  //   "d+": this.getDate(), //日
  //   "h+": this.getHours(), //小时
  //   "m+": this.getMinutes(), //分
  //   "s+": this.getSeconds(), //秒
  //   "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  //   "S": this.getMilliseconds() //毫秒
  // };
  // if (/(y+)/.test(fmt)) {
  //   fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  // }
  // for (let k in o) {
  //   if (new RegExp("(" + k + ")").test(fmt)) {
  //     fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  //   }
  // }
  // return fmt;
};
// 增加天
Date.prototype.addDays = function(value) {
  if (value === undefined) value = 0;
  this.setDate(this.getDate() + value);
};
// 增加月
Date.prototype.addMonths = function(value) {
  if (value === undefined) value = 0;
  this.setMonth(this.getMonth() + value);
};
// 增加年
Date.prototype.addYears = function(value) {
  if (value === undefined) value = 0;
  this.setFullYear(this.getFullYear() + value);
};
// 设置距离当天天数的开始
Date.prototype.setDayFirst = function(value) {
  if (value === undefined) value = 0;
  this.setTime(new Date(this.format("MM DD,YYYY") + " 00:00:00.000").getTime());
  this.setTime(this.getTime() + this.oneDay * value);
};
// 设置当天天数的结束
Date.prototype.setDayLast = function(value) {
  if (value === undefined) value = 0;
  this.setTime(new Date(this.format("MM DD,YYYY") + " 23:59:59.999").getTime());
  this.setTime(this.getTime() + this.oneDay * value);
};
// 设置日期到本周第n天
Date.prototype.setWeek = function(value) {
  if (value === undefined) value = 0;
  let year = this.getFullYear();
  //因为月份是从0开始的,所以获取这个月的月份数要加1才行
  let month = this.getMonth() + 1;
  let date = this.getDate();
  let day = this.getDay();
  console.log(date);
  //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
  if (day !== 0) {
    value = value + (day - 1);
  } else {
    value = value + day;
  }
  if (day) {
    //这个判断是为了解决跨年的问题
    if (month > 1) {
      month = month;
    } else {
      //这个判断是为了解决跨年的问题,月份是从0开始的
      year = year - 1;
      month = 12;
    }
  }
  this.setDate(this.getDate() - value);
  year = this.getFullYear();
  month = this.getMonth() + 1;
  date = this.getDate();
  let newData = new Date(
    year,
    month - 1,
    date,
    this.getHours(),
    this.getMinutes(),
    this.getSeconds(),
    this.getMilliseconds()
  );
  this.setTime(newData.getTime());
};
// 设置距离本周周数的开始
Date.prototype.setWeekFirst = function(value) {
  if (value === undefined) value = 0;
  this.setWeek(1);
  this.setDayFirst(-7 * value);
};
// 设置本周周数的结束
Date.prototype.setWeekLast = function(value) {
  if (value === undefined) value = 0;
  this.setWeek(7);
  this.setDayLast(-7 * value);
};
// 设置距离当月的第一天
Date.prototype.setMonthFirst = function(value) {
  if (value === undefined) value = 0;
  this.setDate(1);
  this.setMonth(this.getMonth() + value);
  this.setDayFirst();
};
// 设置距离当月的最后一天
Date.prototype.setMonthLast = function(value) {
  if (value === undefined) value = 0;
  let currentMonth = this.getMonth();
  let nextMonth = ++currentMonth;
  let nextMonthFirstDay = new Date(this.getFullYear(), nextMonth, 1);
  this.setTime(new Date(nextMonthFirstDay - this.oneDay).getTime());
  this.setMonth(this.getMonth() + value);
  this.setDayLast();
};
// 设置距离今年年数的开始
Date.prototype.setYearFirst = function(value) {
  if (value === undefined) value = 0;
  this.setTime(new Date(this.format("01 01,YYYY") + " 00:00:00.000").getTime());
  this.setFullYear(this.getFullYear() + value);
};
// 设置距离今年年数的结束
Date.prototype.setYearLast = function(value) {
  if (value === undefined) value = 0;
  this.setTime(new Date(this.format("12 31,YYYY") + " 23:59:59.999").getTime());
  this.setFullYear(this.getFullYear() + value);
};
Date.prototype.parseRange = function(day) {
  let ts = new Date(this.getTime());
  let te = new Date(this.getTime());
  switch (day) {
    case "今天":
      ts.setDayFirst();
      te.setDayLast();
      break;
    case "昨天":
      ts.setDayFirst(-1);
      te.setDayLast(-1);
      break;
    case "前天":
      ts.setDayFirst(-2);
      te.setDayLast(-2);
      break;
    case "这周":
    case "这个星期":
      ts.setWeekFirst();
      ts.setWeekLast();
      break;
    case "上个星期":
      ts.setWeekFirst(-1);
      ts.setWeekLast(-1);
      break;
    case "上上个星期":
      ts.setWeekFirst(-2);
      ts.setWeekLast(-2);
      break;
    case "这个月":
      ts.setMonthFirst();
      te.setMonthLast();
      break;
    case "上个月":
      ts.setMonthFirst(-1);
      te.setMonthLast(-1);
      break;
    case "上上月":
    case "上上个月":
      ts.setMonthFirst(-2);
      te.setMonthLast(-2);
      break;
    case "今年":
      ts.setYearFirst();
      te.setYearLast();
      break;
    case "去年":
    case "昨年":
      ts.setYearFirst(-1);
      te.setYearLast(-1);
      break;
    case "前年":
      ts.setYearFirst(-2);
      te.setYearLast(-2);
      break;
    default:
      // 默认今天
      day = "今天";
      ts.setDayFirst();
      te.setDayLast();
      break;
  }
  return [ts, te];
};

export default {
  debug: false, // app是否打印调试信息
  // APP 运行阶段标记 暂未使用
  STAGEINIT: 0, // 加载
  STAGESTART: 1, // 执行行中
  STAGEDONE: 2, // handler结束
  STAGECLOSE: 3, // APP结束,在handler的最终结点时标记该状态
  tpls: {
    welcome: "欢迎使用RSS阅读器技能。",
    help: "RSS订阅，能记录通过语音合成来阅读你订阅的内容。",
    configModel: "{mode}模式设置成功。",
    retryTips: "请重试！",
    retryWithActionTips: "你可执行命令为：{action}。请重试！",
    pleaseSay: "请说出你要{action}的内容。",
    recordOver: "记录结束。",
    recordContinue: "你刚才{action}了：{content}。你还要做什么吗？",
    recordSummary: "当前一共有{count}条记录。",
    recordSummaryDay: "{day}一共有{count}条记录。",
    recordSummaryRange: "{ts}至{te}一共有{count}条记录。",
    actionConfirm: "确认真的要{action}记录？",
    actionOK: "已成功{action}记录。",
    actionCancle: "已取消{action}记录。",
    exploreSpeakRange: "请说出你要查询的范围",
    exploreTips: "你可以使用：上一条、下一条等命令进行浏览记录。",
    exploreRecord: "{date}你记录了{content}。",
    existSkillOK: "RSS订阅技能退出成功！"
  }
};
